package com.handydata.antifraud.listener;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import lombok.extern.slf4j.Slf4j;

import org.dmg.pmml.PMML;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

import com.handydata.antifraud.channel.outbound.DataPusher;
import com.handydata.antifraud.common.task.LoadPmmlRun;
import com.handydata.antifraud.common.task.TaskRun;
import com.handydata.antifraud.common.task.UruleCheckRun;
import com.handydata.antifraud.dao.RiskRuleDictlinfoBasedataDao;
import com.handydata.antifraud.entity.RuleInfoDo;
import com.handydata.antifraud.rule.RuleEngineInvokerFactory;
import com.handydata.antifraud.rule.RuleEngineScheduler;
import com.handydata.antifraud.rule.action.WsInvocationAction;
import com.handydata.antifraud.ws.EncryInfo;
import com.handydata.common.util.RedisKey;
import com.handydata.common.util.RedisUtil;

@Component
@Slf4j
public class StartListener extends TaskRun implements SmartLifecycle {
    @Autowired
    private DataPusher dataPusher;

    @Autowired
    private RuleEngineScheduler ruleEngineScheduler;

    @Autowired
    private WsInvocationAction wsInvocationAction;

    @Autowired
    private EncryInfo encryInfo;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RuleEngineInvokerFactory ruleEngineInvokerFactory;

    @Autowired
    private RiskRuleDictlinfoBasedataDao riskRuleDictlinfoBasedataDao;

    public void run(String... strings)  {
        try{
            log.info(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");
            Future<PMML> futureLoadPmmlRun = this.threadPoolTaskExecutor.submit(new LoadPmmlRun(encryInfo));
            PMML pmml = futureLoadPmmlRun.get();
            log.info("开始训模POMML文件!");
            if(null == pmml){
                throw new Exception("加载PMML文件失败!");
            }
            log.info("开始拿JSON文件...需要几秒钟,稍等。。。");
            Future<Map<String, List<RuleInfoDo>>> uruleCheckMap = this.threadPoolTaskExecutor.submit(new UruleCheckRun(ruleEngineInvokerFactory, riskRuleDictlinfoBasedataDao, encryInfo));
            Map<String, List<RuleInfoDo>> ruleInfoDoMap = uruleCheckMap.get();
            if(null == ruleInfoDoMap){
                throw new Exception("规则匹配校验失败,请检查!");
            }
            if(redisUtil.set(RedisKey.RULE_KNOWLEDGE,ruleInfoDoMap)){
                log.info("规则匹配校验成功!");
            }else{
                throw new Exception("规则匹配校验失败放入缓存失败,重启服务。");
            }
        }catch(Exception ex){
            log.error("加载缓存到redis或者初始化监控队列失败：{}", ex);
        }

    }

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable runnable) {

    }

    @Override
    public void start() {
        run();
    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isRunning() {
        return false;
    }

    @Override
    public int getPhase() {
        return 0;
    }
}